package com.lx.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.lx.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000117676113";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCgF+ZHwG9/jmo6UA1VtRxaGga6SQ8gxyD+CarfxzQFIlPS7Z8jiGl2TBaDgm0YE7+zEaL6g786QDEhq4EQprDtUsOJs9m98kSEqpgpsbr9xJF9DYrjZk5gyvCmZQjS/BEyRctunJ13CVpsUcpjUpyE/EZU/c1Bh9+Mnd/sJhy/EWtdXHKZHuCNeU46XRLgpXa6gPbrorKy/bcvJcZruzwwoxdaZ6itV1E24y3QO1XVrL5urJSvtUlt76qPF0eVY59r/X6g/+M/+fVyAqO3VevzkjFULQLOejll/UyBiuOLUN3F57M3lyIZhDVcb5vI8Gp25uXbyop7xPYPUPmfJJmPAgMBAAECggEAWsbSe7MBRhnR4DjOJuAoMGr7j5unqVVgPsvqiTMiKxsY3syAZRyPeFWSmoVVKMbh9z6z5EraHsSRV18xMujh/9/jImzJtavu8C7vUw0FU6mnOXp5W5ICNV49cIxvoXruoUv0G0dQ/lNeY3dJRGJuf/WwR7cWMOdDei927id6O1hVzAX8ppbO7+L4IhLsMpFpeGgjN/Blfl24JuvJeYjX246ddT2iQVM4TNJFRMDxwteA7SAwbfa9UE/1T5DufcQTPEuGqoN+t3tTc9BpshEGv5fnlLJlpt8VcBXbMA31zBoi/yDP1nPMqHPYG/cQHWeKj2x7YTu2JqJUEs6KcXQWSQKBgQDc5xcZjHqrtL6pgweLTC8EIouWZpD6VjPzaRt6fQ5T1P+1gP7eUMm3LeYhpVAtvrXudf55XgKhw96e2roSnWdSLFjBtyxU6V16mlAx8+cPFFGceN0jEnfPMI+dyJWxb9kkFQO1HKP4hMQM53d5w+Q1WiGgeGG7zfUGym04i8UOAwKBgQC5h3lckpK2N1XoAc2dpE9YgaQalvEZGT9a/QVzcfEpHHvokgaF7xdpIxm7gwGKyDxGczL71CcRu6b1ZsPvNp2s25HJZxuLO0hTMhvEE3pD592nsZ0HGJIy4eemPGXWfaY7X529SQ9fM+4Rp+QiPKhYeTvS/9fHVwBaJm3uTvrGhQKBgQC9emKwAXU/TvuzvrZ73078xvzgdv2oHMTW5rMxL49YjP0BHLKA0t8TBoW9GmVZt5mp/v/V7rNnOfD8ZTTxvp3fODaxgoK+GqWaAj8DeegZYR5Xjrxv1ul5iHfHqUPtPLkOYBAcC4MCKi9Xujsa8NIQiU0Krfd29cRUnNFujUTrLQKBgQCovqeLo2T9hucD1mAF+eSRQ5jW9mLjySruvyOuFPybv+STa/2y4/wPDHXjLwzABc+M5EqC8jt2zY7VtVqQwMQ47Jx06usKgCkQQosuk+ioysO4OrvwMFEJJp9je5vEsxP2/Mij2Ue596mZNXMyEKmtjtby2vCPxnnzhPCHyB4fYQKBgFHssiSU8g3aEAEb0YbWEqHCtqEYOX7442Rgs8K+aE9SgvSc+s1gWLA2DemhQA43d6dyK/WsVTGL2IUwHobUyu12ZbUIW/OokhJFIJmu1gsah0G2Rqs6QxpCdix3Wm/hKeZvHWugjYY0mLXrOHS94GW05SfFZEw+P/4dxSe9sVwB";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmAOpP+GzjN2i1IHLrx5zecg90I17bz0inpBleyZUmUrE0tLHebXp5XaLbt2J2me8Q8igD7eIofYZLOLTV3g+UIzD+RzbSFFuO+UaXcBICPEhZWvjcisLPYeTo7pCRbg8auglLn30irmcRu+bsuezQfr2dCLVI4OGv3R/vxnVMYO/KCC1zG5nnQW0A+Z1S23c7R0jBfZKX77wY6pwatc1qoX2b1o85Th22ZF1UvlVn0JJQt2eDSQ8v1ZP3JK0tl958iqhx1bQqRsMdQ8OzKvlm8z7mrfYpIx9lOEWmeyu296henVPaFoy7XXeJoED9jjiOt3iVyCNPxFjU2qyevqe1QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://zwm34b.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
